﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Inets
{
    class Udp
    {
		private static UdpClient client;
		private static IPEndPoint localep = new IPEndPoint(IPAddress.Any, 0);

		/// <summary>
		/// "Connects" to an UDP server.
		/// </summary>
		/// <param name="host">The hostname or IP to connect to.</param>
		/// <param name="port">The port to connect to.</param>
		public static void Connect(string host, int port)
		{
			client = new UdpClient(host, port);
		}

		/// <summary>
		/// Closes the UdpClient.
		/// </summary>
		public static void Disconnect()
		{
			client.Close();
		}

		/// <summary>
		/// Returns the active UdpClient.
		/// </summary>
		/// <returns>The active UdpClient.</returns>
		public static UdpClient GetUdpClient() { return client; }

		/// <summary>
		/// Returns the active socket.
		/// </summary>
		/// <returns>The active socket.</returns>
		private static Socket GetSocket() { return client.Client; }

		/// <summary>
		/// Gets a datagram converted to a string from the connection you "connected" to using Connect(). Won't return until data has been received.
		/// </summary>
		/// <returns>A string containing the received data.</returns>
		public static string ReadString()
		{
			return Encoding.ASCII.GetString(ReadBytes());
		}

		/// <summary>
		/// Gets a datagram from the connection you "connected" to using Connect(). Won't return until data has been received.
		/// </summary>
		/// <returns>A byte array containing the received data.</returns>
		public static byte[] ReadBytes()
		{
			return client.Receive(ref localep);
		}

		/// <summary>
		/// Sends a string of text to the "connected" server.
		/// </summary>
		/// <param name="text">The string to send.</param>
		public static void Send(string text)
		{
			Send(Encoding.ASCII.GetBytes(text));
		}

		/// <summary>
		/// Sends a byte array to the "connected" server.
		/// </summary>
		/// <param name="bytes">The byte array to send.</param>
		public static void Send(byte[] bytes)
		{
			client.Send(bytes, bytes.Length);
		}
	}
	class Tcp
	{
		private static TcpClient client;
		private static IPEndPoint localep = new IPEndPoint(IPAddress.Any, 0);

		/// <summary>
		/// Connects to a TCP server.
		/// </summary>
		/// <param name="host">The hostname or IP to connect to.</param>
		/// <param name="port">The port to connect to.</param>
		public static void Connect(string host, int port)
		{
			client = new TcpClient(host, port);
		}

		/// <summary>
		/// Disconnects from a TCP server.
		/// </summary>
		public static void Disconnect()
		{
			client.Close();
		}

		/// <summary>
		/// Returns the active TcpClient.
		/// </summary>
		/// <returns>The active TcpClient.</returns>
		public static TcpClient GetTcpClient() { return client; }

		/// <summary>
		/// Returns the active socket.
		/// </summary>
		/// <returns>The active socket.</returns>
		public static Socket GetSocket() { return client.Client; }
	}
}
